package com.lm.controller;

import com.lm.dao.BookMapper;
import com.lm.pojo.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class HelloController {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private BookMapper bookMapper;

    @RequestMapping("/hello")
    public String test(){
        return "test";
    }

    @RequestMapping("/list")
    List<Map<String,Object>> list(){
        String sql = "select * from books";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
        return maps;
    }

    @RequestMapping("/getAllBooks")
    List<Book> getAllBooks(){
        return bookMapper.getAllBooks();
    }

    @RequestMapping("/updateBatch")
    String updateBatch(){
        List<Map<String,Object>> list = new ArrayList<>();
        Map<String,Object> map1 = new HashMap<>();
        map1.put("bookID",2);
        map1.put("bookCounts",1);
        map1.put("detail","MySQL");
        map1.put("detail","MySQL");
        list.add(map1);

        Map<String,Object> map2 = new HashMap<>();
        map2.put("bookID",3);
        map2.put("bookCounts",1);
        map2.put("detail","test2");
        list.add(map2);

        Map<String,Object> map3 = new HashMap<>();
        map3.put("bookID",3);
        map3.put("bookCounts",1);
        map3.put("detail","test2");
        list.add(map2);

        int ii = bookMapper.updateBatch(list);
        System.out.println("ii：" + ii);
        return "OK";
    }

}
